export default {
  data(){
    return {
      dataKey: 1,
      headHeight: uni.getSystemInfoSync().statusBarHeight + 44,
      currentTab: ''
    }
  },
  methods: {
    elementInView(offsettop) {
      let arr = []
      let rect = this.serviceList
      let index = 0
      for(let i = 0; i < rect.length; i++) {
        if(i <= rect.length - 2) {
          if((offsettop - rect[i].offsetTop) + 140 > 0) {
            arr.push(i)
          }
        } else {
          let h = rect[rect.length - 1].offsetTop - rect[rect.length - 2].offsetTop + 10
          if((offsettop - rect[i].offsetTop) + h  > 0) {
            arr.push(i)
          }
        }
      }
      // 如果有锚点则调到锚点位置
      if(this.anchor > -1) {
        this.serviceActive = Number(this.anchor)

        if(!this.serviceList[this.anchor] || this.serviceList[this.anchor].offsetTop < 1) {
          this.$nextTick(() => {
            let t = setTimeout(() => {
              this.getViewRect()
              uni.pageScrollTo({
                duration: 0,
                scrollTop: this.serviceList[this.anchor].offsetTop - 139,
                success: () => {
                  this.anchor = -1
                  clearTimeout(t)
                }
              })
            }, 200)
          })
        } else {
          uni.pageScrollTo({
            duration: 0,
            scrollTop: this.serviceList[this.anchor].offsetTop - 139,
            success: () => {
              this.anchor = -1
            }
          })
        }

      } else {
        this.serviceActive = arr.length > 0 ? arr.length - 1 : 0
      }
    },
    // 点击左侧菜单
    onClickLeftMenu(data, index) {
      this.currentTab = ''
      this.serviceActive = index
      uni.pageScrollTo({
        duration: 0,
        scrollTop: data.offsetTop - 139
      })
      setTimeout(() => {
        this.currentTab = Number(index)
        setTimeout(() => {
          this.currentTab = ''
        }, 1000)
      }, 100)
    },
    // 获取布局信息
    getViewRect() {
      uni.createSelectorQuery().selectAll('.right-item').boundingClientRect().exec(rect => {
        rect[0].map((item, index) => {
          let serviceItem = this.serviceList[index]
          serviceItem.offsetTop = this.scrollTop + item.top

          this.$set(this.serviceList, index, serviceItem)
        })
      })
    },
    // 递归给数组添加服务数量的字段
    handleAddAmount(list, arr) {
      list.forEach(item => {
        if (item.showMulSelected) {
          // 结束条件：has_Children为false
          if (item.has_Children) {
            this.handleAddAmount(item.maintenanceTypeAndGoodsChildList, arr)
          } else {
            if (item.maintenanceGoodsLists) {
              item.maintenanceGoodsLists.forEach(ser => {
                if(arr.length > 0) {
                  try{
                    arr.map(a => {
                      ser.amount = 0
                      if(ser.id === a.id) {
                        ser.amount = a.amount
                        throw new Error('ending')
                      }
                    })
                  }catch(e){
                  }
                } else {
                  ser.amount = 0
                  ser.key = this.dataKey
                  this.dataKey++
                }
              })
            }
          }
        } else {
          // 结束条件：has_Children为false
          if (item.has_Children) {
            this.handleAddAmount(item.maintenanceTypeAndGoodsChildList, arr)
          } else {
            if (item.maintenanceGoodsLists) {
              item.maintenanceGoodsLists.forEach(ser => {
                if(arr.length > 0) {
                  try{
                    arr.map(a => {
                      ser.amount = 0
                      if(ser.id === a.id) {
                        ser.amount = a.amount
                        throw new Error('ending')
                      }
                    })
                  }catch(e){
                  }
                } else {
                  ser.amount = 0
                  ser.key = this.dataKey
                  this.dataKey++
                }
              })
            }
          }
        }

      })
    },
    // 减
    less(item, pItem) {
      if (item.amount < 1) return
      item.amount -= 1
      this.handleCheckLength() // 计算已选数量
    },

    // 加
    add(item, pItem) {
      item.amount += 1
      // 添加数量判断，如果不是勾选服务项
      if (!pItem.showMulSelected) {
        this.handleCheckLength()
      } else {
        this.handleCheckLength()
      }
      // this.checkedVal.id = pItem.id
    },

    // 选择服务，勾选服务
    selectedServe(item, pItem) {
      // this.isChecked = item.id
      item.isChecked = !item.isChecked
      item.maintenanceGoodsLists.map(i => {
        i.amount = item.isChecked ? 1 : 0
      })
      this.handleCheckLength()

      this.$nextTick(() => {
        // this.getNodesInfo()
      })
    },
    // 查询是否勾选了5项，和 计算左侧列表已选数量
    handleCheckLength(data) {
      let checkedList = []
      // 遍历数组，查询勾选项有几个
      this.list.forEach(item => {
        item.serCount = 0 // 左侧选中数量清0
        // 如果是勾选项
        if (item.has_Children && item.showMulSelected) {
          item.maintenanceTypeAndGoodsChildList.forEach(ser => {
            // 如果是勾选，判断是否勾选
            let isChecked = ser.maintenanceGoodsLists.some(i => {
              return i.amount > 0
            })
            if(ser.maintenanceGoodsLists.length > 0) {
              ser.isChecked = isChecked
            }

            if (ser.isChecked) {
              if(isChecked) {
                item.serCount += 1 // 左侧选中数量重新计算
              } else {
                // ser.isChecked = false
                // ser.maintenanceGoodsLists.map(i => {
                //   i.amount = 1
                // })
              }
            }
          })
          checkedList.push(item)
          // 处理美容服务已选数量，不是勾选项
        } else if(item.has_Children && !item.showMulSelected) {
          item.maintenanceTypeAndGoodsChildList.forEach(ser => {
            ser.maintenanceGoodsLists.forEach(serItem => {
              if (serItem.amount !== 0) {
                item.serCount += 1 // 左侧选中数量重新计算
                // checkedList.push(serItem)
              }
            })
          })

          checkedList.push(item)
          // 处理洗车已选数量  不是勾选项
        } else {
          item.maintenanceGoodsLists.forEach(ser => {
            if (ser.amount !== 0) {
              item.serCount += 1 // 左侧选中数量重新计算
            }
          })
          checkedList.push(item)
        }
      })
      // this.checkedList = checkedList
      this.$nextTick(() => {
        setTimeout(() => {
          this.getViewRect()
        }, 500)
      })
      if(data) {
        setTimeout(() => {
          for(let i in this.serviceListSelectNum) {
            if(this.serviceListSelectNum[i] > 0) {
              this.onClickLeftMenu(this.serviceList[i], i)
              break
            }
          }
        }, 500)
      }
      this.screenSelectType(checkedList)
    },
    // 筛选已选类型
    screenSelectType(list) {
      list = JSON.parse(JSON.stringify(list))
      let checkedList = []
      // 遍历数组，查询勾选项有几个
      checkedList = list.filter(item => {
        if (item.serCount > 0) {
          if(item.maintenanceGoodsLists) {
            item.maintenanceGoodsLists = item.maintenanceGoodsLists.filter(i => {
              return i.amount > 0
            })
          } else if(item.has_Children && item.showMulSelected) {
            item.maintenanceGoodsLists = []
            item.maintenanceTypeAndGoodsChildList.map(ser => {
              // 如果是勾选，判断是否勾选
              if (ser.isChecked) {
                ser.maintenanceGoodsLists.forEach(serItem => {
                  if (serItem.amount !== 0) {
                    item.maintenanceGoodsLists.push(serItem)
                  }
                })
              }
            })
          } else if(item.has_Children && !item.showMulSelected) {
            item.maintenanceGoodsLists = []
            item.maintenanceTypeAndGoodsChildList.map(ser => {
              ser.maintenanceGoodsLists.map(serItem => {
                if (serItem.amount !== 0) {
                  item.maintenanceGoodsLists.push(serItem)
                }
              })
            })
          }
        }
        return item.serCount > 0
      })
      this.cartList = checkedList
      // console.log(checkedList)
    },
    // 购物车弹窗数量修改
    onChangeNum(data) {
      let current = null, pitem = null

      this.list.map(item => {
        // 如果是勾选项
        if (item.has_Children && item.showMulSelected) {
          item.maintenanceTypeAndGoodsChildList.forEach(ser => {
            if(ser.maintenanceGoodsLists) {
              ser.maintenanceGoodsLists.forEach(serItem => {
                if(data.type === 'clear') {
                  ser.isChecked = false
                  serItem.amount = 0
                } else {
                  if(serItem.id === data.data.id && item.id === data.pid && serItem.key === data.data.key) {
                    current = serItem
                    pitem = item
                    if(data.type === 'less') {
                      serItem.amount -= 1
                    } else {
                      serItem.amount += 1
                    }
                  }
                }
              })
            }
          })
        } else if(item.has_Children && !item.showMulSelected) {
          item.maintenanceTypeAndGoodsChildList.forEach(ser => {
            ser.maintenanceGoodsLists.forEach(serItem => {
              if(data.type === 'clear') {
                serItem.amount = 0
              } else {
                if(serItem.id === data.data.id && item.id === data.pid && serItem.key === data.data.key) {
                  current = serItem
                  pitem = item
                  if(data.type === 'less') {
                    serItem.amount -= 1
                  } else {
                    serItem.amount += 1
                  }
                }
              }
            })
          })
        } else {
          item.maintenanceGoodsLists.map(ser => {
            if(data.type === 'clear') {
              ser.amount = 0
            } else {
              if(ser.id === data.data.id && item.id === data.pid && serItem.key === data.data.key) {
                current = ser
                pitem = item
                if(data.type === 'less') {
                  ser.amount -= 1
                } else {
                  ser.amount += 1
                }
              }
            }
          })
        }
      })

      this.handleCheckLength()

      // if(data.type === 'less') {
      //   this.less(current, pitem)
      // }
      // if(data.type === 'add') {
      //   this.add(current, pitem)
      // }
    },
    // 点击变更
    onChangeGoods(item, pitem){
      uni.navigateTo({
        url: `/pages_commodity/doMaintenance/replaceProduct?id=${item.id}&carId=${this.myLoveCar.car_Id}&maintenanceTypeId=${item.maintenance_Type_Id}&pid=${pitem.id}`
      })
    },
    // 替换产品
    replaceProduct(data) {
      let res = data.data
      let num = 0
      try{
        this.list.map((item, idx) => {
          if (item.has_Children && item.showMulSelected) {
            item.maintenanceTypeAndGoodsChildList.forEach(ser => {
              if(ser.maintenanceGoodsLists && ser.maintenanceGoodsLists.length > 0) {
                ser.maintenanceGoodsLists.forEach((sItem, index) => {
                  if(sItem.maintenance_Type_Id === data.maintenance_Type_Id && item.id == data.pid) {
                    res[num].amount = sItem.amount
                    Object.assign(sItem, res[num])
                    num++
                    // sItem.amount = 10
                    if(num >= res.length) {
                      throw new Error('ending')
                    }
                  }
                })
              }
            })
          }
        })
      }catch(e){

        this.handleCheckLength()
        //TODO handle the exception
        console.log(e)
      }
    },
    h5Pagescroll() {
      uni.createSelectorQuery().in(this).select('.sticky').boundingClientRect(data => {
        this.isSticky = data.top <= this.navHeight
      }).exec()
    }
  }
}